home *** CD-ROM | disk | FTP | other *** search
- void arcdif(buf)
- char buf[280];
- {
- #include "nbstime.h"
- #include <stdio.h>
- #ifdef IBMPC
- #include <dos.h>
- #if defined(MSC)
- struct dosdate_t date;
- struct dostime_t time;
- #endif
- #endif
- #ifdef SUN
- #include <sys/time.h>
- #include <math.h>
- #endif
- char c;
- int j,yr,mo,day,hr,min,sec,dst; /* holds parsed NBS time */
- #ifdef IBMPC
- int yribm,moibm,dayibm,hribm,minibm,secibm,hunibm; /* holds computer time*/
- int yrat,moat,dayat,hrat,minat,secat;/* holds CMOS time for AT*/
- #endif
- #ifdef SUN
- long int mjd;
- long int mjd0 = 40587; /* mjd of 1/1/70 */
- struct timeval tvv,*tp;
- #endif
- double diff;
- #ifdef IBMPC
- extern int utcdif; /* local time - utc in hours */
- extern int dsflag; /* daylight saving time? 1=yes, 0=no */
- extern int atflag; /* AT-type machine? 1=yes, 0=no */
- #endif
- extern FILE *jop; /* file handle for writing difference*/
- #ifdef IBMPC
- static int lday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /*last day of month*/
- static int tday[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
- int unpbcd();
- int dsdone = 0; /* flag to show day. sav. time corr. done */
- #endif
- int done = 0; /* flag to show when comparison is done */
- /*
- this subroutine receives a time string in character
- array buf. it is parsed and compared with the system
- clock. the origin of the parser is the - character between
- the year and the month so that leading stuff (possible a lf)
- will simply be ignored.
-
- if IBMPC is true, then the comparison is made using local
- time as defined by global variables dsflag to check for
- daylight savings time and utcdif to convert from UTC to local
- time. if SUN is true then the conversion is done using UTC
- directly.
-
- begin by getting computer time now.
- */
- #ifdef IBMPC
- #if defined(MSC)
- _dos_gettime(&time);
- hribm=time.hour;
- minibm=time.minute;
- secibm=time.second;
- hunibm=time.hsecond;
- _dos_getdate(&date);
- yribm=date.year;
- moibm=date.month;
- dayibm=date.day;
- yribm -= 1900;
- #else
- _AH=0x2c;
- geninterrupt(0x21);
- hribm=_CH;
- minibm=_CL;
- secibm=_DH;
- hunibm=_DL;
- _AH=0x2a;
- geninterrupt(0x21);
- yribm=_CX;
- moibm=_DH;
- dayibm=_DL;
- yribm -= 1900;
- /*
- if this is an at-type machine, read CMOS clock too
- */
- if(atflag != 0)
- {
- _AH=2;
- geninterrupt(0x1a);
- hrat=_CH;
- minat=_CL;
- secat=_DH;
- _AH=4;
- geninterrupt(0x1a);
- yrat=_CL;
- moat=_DH;
- dayat=_DL;
- }
- #endif
- #endif
- #ifdef SUN
- tp= &tvv;
- gettimeofday(tp,0);
- #endif
- /*
- now parse line from NBS transmission
-
- first find - between year and month
- */
- for(j=0; (buf[j] != 0) && (buf[j] != '-') ; j++) ;
- sscanf(&buf[j-2],"%2d-%2d-%2d %2d:%2d:%2d %d",&yr,&mo,&day,
- &hr,&min,&sec,&dst);
- #ifdef SUN
- sscanf(&buf[j-8],"%5ld",&mjd); /* also get mjd for SUN */
- /*
- convert nbs time to number of seconds since 1/1/70
- */
- mjd= 86400*(mjd - mjd0);
- mjd= mjd + 3600*hr + 60*min + sec;
- #endif
- #ifdef IBMPC
- /*
- for the IBMPC version, the comparison is made against
- local time -- thus nbs time must be converted to local
- time using utcdif and dst flags.
- */
- /*
- make standard-time portion of dst flag contiguous
- */
- if(dst == 0) dst = 100;
- /*
- convert from utc to local time. note that minute and second
- are already correct.
- daylight savings time flag must also be updated
- if conversion to local time changes the day
-
- **
- ** version of 12 may -- compiled and tested 31 may
- daylight savings time correction must be done in two
- parts since adding hour may cause hour/day to overflow.
- therefore deal with part of it now -- if day changes then
- correction flag dst will change and therefore correction
- for transition days must be done after final day is known.
- also see subroutine parset where same problem appears.
- */
- if( (yr & 3) == 0 ) lday[2]=29; /* 29 days for Feb in leap year
- */
- hr += utcdif; /* convert hour to local time */
- if( (dsflag != 0) && (dst <= 50) && (dst > 1) )
- {
- hr++;
- dsdone=1;
- } /* do first part of daylight savings time */
- if(hr < 0)
- {
- hr += 24;
- day--;
- dst++; /* update daylight savings flag for change of day*/
- if(day < 1)
- {
- mo--;
- if(mo < 1)
- {
- mo=12;
- yr--;
- }
- day=lday[mo];
- }
- }
- if(hr > 23)
- {
- hr -= 24;
- day++;
- dst--; /* update daylight saving flag for change of day */
- if(day > lday[mo])
- {
- day=1;
- mo++;
- if(mo > 12)
- {
- mo=1;
- yr++;
- }
- }
- }
- /*
- now finish up daylight savings time if enabled
- */
- if ( (dsflag != 0) && (dsdone == 0) )
- {
- if( (dst == 51) && (hr >= 2) ) hr++;
- if( (dst == 1) && (hr < 2) ) hr++;
- }
- #endif
- /*
- ready to begin comparing the clocks
- */
- fprintf(jop,"%2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
- yr,mo,day,hr,min,sec);
- #ifdef SUN
- /*
- sun comparison is done directly as difference
- of two longs giving number of seconds since 1/1/70
- */
- diff= tp->tv_sec - mjd;
- done=0;
- if(fabs(diff) > 100000.)
- {
- fprintf(jop," %8.2fd\n",diff/86400.);
- done=1;
- }
- if( (done == 0) && (fabs(diff) > 10000.) )
- {
- fprintf(jop," %8.2fh\n",diff/3600.);
- done=1;
- }
- if( (done == 0) && (fabs(diff) > 1000.) )
- {
- fprintf(jop," %8.2fm\n",diff/60.);
- done=1;
- }
- if( (done == 0) && (fabs(diff) <= 1000.) )
- {
- diff += (float) tp->tv_usec/1000000.;
- fprintf(jop," %8.2fs\n",diff);
- }
- #endif
- #ifdef IBMPC
- /*
- ibm comparison is done bit by bit to preserve
- resolution
- */
- diff=365*(yribm - yr) + tday[moibm] - tday[mo] + dayibm - day;
- if( ( (yribm & 3) == 0) && (moibm > 2) ) diff++;
- if( ( (yr & 3) == 0) && (mo > 2) ) diff--;
- if( (diff > 1) || (diff < -1) )
- {
- fprintf(jop," %8.2fd",diff);
- done=1;
- }
- if(done == 0)
- {
- diff= 24*diff + hribm - hr;
- if( (diff > 2) || (diff < -2) )
- {
- fprintf(jop," %8.2fh",diff);
- done=1;
- }
- }
- if(done == 0)
- {
- diff=60*diff + minibm - min;
- if( (diff > 10) || (diff < -10) )
- {
- fprintf(jop," %8.2fm",diff);
- done=1;
- }
- }
- if(done == 0)
- {
- diff=60*diff + secibm - sec + 0.01*(float)hunibm;
- fprintf(jop," %8.2fs",diff);
- }
- if(atflag == 0)
- {
- fprintf(jop," 0.00\n");
- fclose(jop);
- return;
- }
- /*
- begin comparison of CMOS time -- first convert from packed
- BCD to normal binary, then compare as above.
- */
- yrat=unpbcd(yrat);
- moat=unpbcd(moat);
- dayat=unpbcd(dayat);
- hrat=unpbcd(hrat);
- minat=unpbcd(minat);
- secat=unpbcd(secat);
- diff=365*(yrat - yr) + tday[moat] - tday[mo] + dayat - day;
- if( ( (yrat & 3) == 0) && (moat > 2) ) diff++;
- if( ( (yr & 3) == 0) && (mo > 2) ) diff--;
- done=0;
- if( (diff > 1) || (diff < -1) )
- {
- fprintf(jop," %8.2fd\n",diff);
- done=1;
- }
- if(done == 0)
- {
- diff=24*diff + hrat - hr;
- if( (diff > 2) || (diff < -2) )
- {
- fprintf(jop," %8.2fh\n",diff);
- done=1;
- }
- }
- if(done == 0)
- {
- diff=60*diff + minat - min;
- if( (diff > 10) || (diff < -10) )
- {
- fprintf(jop," %8.2fm\n",diff);
- done=1;
- }
- }
- if(done == 0)
- {
- diff=60*diff +secat - sec;
- fprintf(jop," %8.2fs\n",diff);
- }
- #endif
- fclose(jop);
- }
-